package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private DeptService deptService;


    @Autowired
    private EmpExprService empExprService;

    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));

        // 借助于 lambdaQuery() 方法，构建查询条件，同时获取分页结果
        page = lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(), Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd())
                .page(page);

        List<Emp> emps = page.getRecords();

        // 遍历emps结果，添加部门名称
        emps.forEach(e -> {
            Dept dept = deptService.getById(e.getDeptId());
            if (dept != null && !dept.getName().isEmpty()) {
                e.setDeptName(dept.getName());
            }
        });

        return new PageResult<>(page.getTotal(), emps);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        emp.setPassword("123456");
        this.save( emp);
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList!=null && exprList.size()>0){
            for (EmpExpr empExpr : exprList) {
//                empExpr.setEmpId(emp.getId());
                empExprService.saveBatch(emp.getExprList());
            }
        }

    }

    @Override
    public Emp getByEmpId(Integer id) {
        //通过id查询员工信息
        Emp emp = this.getById(id);
        //用LambdaQueryWrapper查询员工工作经历
        LambdaQueryWrapper<EmpExpr> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EmpExpr::getEmpId,emp.getId());
        //调用方法执行查询多条员工经历
        List<EmpExpr> empExprList = empExprService.list(lqw);
        emp.setExprList(empExprList);
        return emp;
    }

    @Override
    public void updateEmpById(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        //更新员工信息
        this.updateById(emp);
        //删除工作经历通过LambdaQueryWrapper
        LambdaQueryWrapper<EmpExpr> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EmpExpr::getEmpId,emp.getId());
        empExprService.remove(lqw);

        //新增
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList!=null && exprList.size()>0){
            for (EmpExpr empExpr : exprList) {
                empExpr.setEmpId(emp.getId());
            }
            empExprService.saveBatch(exprList);
        }
    }

    @Override
    public void deleteByEmpIds(List<Integer> ids) {
        this.removeBatchByIds(ids);
        //delete from emp_expr where emp_id in (ids)
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }


}
